﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;
using System.IO;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_edit : System.Web.UI.Page
    {
        private Guid _applicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _applicationId = new Guid(Request.QueryString["id"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                if (_applicationId != Guid.Empty)
                {
                    SmartSpace.Core.Site application = service.GetSite(_applicationId);

                    //_ctlEditHostUrl.Text = application.HostUrl;
                    _ctlEditText.Text = application.Name;
                    _ctlEditCopyright.Text = application.Copyright;
                    _ctlHiddenImageId.Value = application.ImageId.ToString();

                    // 获取可用的主题
                    List<SiteTemplate> themes = service.GetSiteTemplates(application.Type);
                    _ctlListTheme.DataSource = themes;
                    _ctlListTheme.DataBind();
                    _ctlListTheme.SelectedValue = application.TemplateId.ToString();

                    _ctlCheckAnonymousAccess.Checked = application.IsAllowAnonymous;
                    if (application.AnonymousUserId != null)
                    {
                        User anonymousUser = service.GetUser(application.AnonymousUserId.Value);
                        if (anonymousUser != null)
                        {
                            _ctlAnonymousUser.Text = anonymousUser.Name;
                            _ctlAnonymousUserId.Value = anonymousUser.Id.ToString();
                        }
                    }

                    PagedList<SmartSpace.Core.Page> pages = service.GetPages(_applicationId, 0, 999);
                    _ctlListDefaultPage.DataSource = pages;
                    _ctlListDefaultPage.DataBind();
                    _ctlListDefaultPage.SelectedValue = application.DefaultPageId.ToString();
                }
            }
            _ctlLinkBack.NavigateUrl = "./application-list.aspx";
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();

            Site app = service.GetSite(_applicationId);
            //app.HostUrl = _ctlEditHostUrl.Text;
            app.Name = _ctlEditText.Text;
            app.TemplateId = new Guid(_ctlListTheme.SelectedValue);
            app.Type = service.GetSiteTemplate(app.TemplateId).Type;
            app.Copyright = _ctlEditCopyright.Text;

            app.IsAllowAnonymous = _ctlCheckAnonymousAccess.Checked;
            if (!string.IsNullOrEmpty(_ctlAnonymousUserId.Value))
            {
                app.AnonymousUserId = new Guid(_ctlAnonymousUserId.Value);
            }

            if (!string.IsNullOrEmpty(_ctlListDefaultPage.SelectedValue))
            {
                app.DefaultPageId = new Guid(_ctlListDefaultPage.SelectedValue);
            }

            if (_ctlFileImage.HasFile)
            {
                SmartSpace.Core.Image image = new SmartSpace.Core.Image();
                image.Id = Guid.NewGuid();
                image.Caption = _ctlFileImage.FileName;
                image.Data = _ctlFileImage.FileBytes;
                image.Extension = new FileInfo(_ctlFileImage.FileName).Extension;

                if (service.SaveImage(image))
                {
                    app.ImageId = image.Id;
                    service.SaveSite(app);

                    Response.Redirect(_ctlLinkBack.NavigateUrl);
                }
            }
            else
            {
                app.ImageId = new Guid(_ctlHiddenImageId.Value);
                service.SaveSite(app);

                Response.Redirect(_ctlLinkBack.NavigateUrl);
            }
        }
    }
}